class Program { static void Main() { var actions = new List<Action>();
for (int i = 0; i < 3; i++) { actions.Add(() => Console.WriteLine(i)); }
foreach (var action in actions) action(); } }
Варианты ответа: A)
1 2
B)
3 3
C)
0 0
D)
---
✅ Правильный ответ: B
Почему: Лямбда-функции захватывают переменную i по ссылке, а не её значение на каждой итерации. После завершения цикла i == 3, и все замыкания ссылаются на одно и то же i. Это классическая ловушка замыканий в C#.
class Program { static void Main() { var actions = new List<Action>();
for (int i = 0; i < 3; i++) { actions.Add(() => Console.WriteLine(i)); }
foreach (var action in actions) action(); } }
Варианты ответа: A)
1 2
B)
3 3
C)
0 0
D)
---
✅ Правильный ответ: B
Почему: Лямбда-функции захватывают переменную i по ссылке, а не её значение на каждой итерации. После завершения цикла i == 3, и все замыкания ссылаются на одно и то же i. Это классическая ловушка замыканий в C#.
BY C# 1001 notes
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283
Telegram today rolling out an update which brings with it several new features.The update also adds interactive emoji. When you send one of the select animated emoji in chat, you can now tap on it to initiate a full screen animation. The update also adds interactive emoji. When you send one of the select animated emoji in chat, you can now tap on it to initiate a full screen animation. This is then visible to you or anyone else who's also present in chat at the moment. The animations are also accompanied by vibrations. This is then visible to you or anyone else who's also present in chat at the moment. The animations are also accompanied by vibrations.